package com.brave.manager.controller;

import com.brave.manager.domian.Teacher;
import com.brave.manager.service.TeacherService;

import javax.sound.midi.Soundbank;
import java.util.Scanner;

public class TeacherController {
    Scanner S = new Scanner(System.in);
    TeacherService TeacherService = new TeacherService();

    public void start() {

        TeacherLoop:
        while (true) {
            System.out.println("--------欢迎来到 <老师> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加老师  2.删除老师  3.修改老师  4.查看老师  5.退出");
            String select = S.next();

            switch (select) {
                case "1":
                    addTeacher();
                    break;
                case "2":
                    deleteTeacher();
                    break;
                case "3":
                    updateTeacher();
                    break;
                case "4":
                    findTeacher();
                    break;
                case "5":
                    System.out.println("感谢您使用教师管理系统，再见！");
                    break TeacherLoop;
                default:
                    System.out.println("您输入得有误，请重新输入！");
                    break;
            }
        }
    }

    private void updateTeacher() {
        String id = inputTeacherId();
        Teacher tea = inputTeacherInfo(id);
        TeacherService.updateTeacher(id,tea);
        System.out.println("修改成功！");
    }

    private void deleteTeacher() {
        String id = inputTeacherId();
        TeacherService.deleteTeacher(id);
        System.out.println("删除成功！");
    }

    private String inputTeacherId() {
        String id ;

        while (true){
            System.out.println("请输入教师工号：");
            id= S.next();
            boolean res = TeacherService.isExists(id);
            if(!res){
                System.out.println("该工号不存在，请重新输入：");
            }else {
                break;
            }
        }
        return  id;
    }

    private void findTeacher() {
        Teacher[] teas = TeacherService.findAllTeacher();
        if(teas== null){
            System.out.println("查无信息，请重新输入！");
            return;
        }
        System.out.println("工号\t\t姓名\t年龄\t性别");
        for (int i = 0; i < teas.length; i++) {
            Teacher Teacher = teas[i];
            if (Teacher != null) {
                System.out.println(Teacher.getId() + "\t\t" + Teacher.getName() + "\t\t" + Teacher.getAge() + "\t\t" + Teacher.getSex());

            }
        }
    }

    private void addTeacher() {
        String id;

        while (true) {
            System.out.println("请输入教师学号：");
            id = S.next();

            boolean res = TeacherService.isExists(id);
            if (res) {
                System.out.println("您输入的学号已存在，请重新输入：");
            } else {
                break;
            }
        }
        Teacher tea = inputTeacherInfo(id);
        boolean result = TeacherService.addTeacher(tea);
        if (result) {
            System.out.println("添加成功！");
        } else {
            System.out.println("添加失败！");
        }

    }

    private Teacher inputTeacherInfo(String id) {
        System.out.println("请输入教师姓名：");
        String name = S.next();
        System.out.println("请输入教师年龄：");
        String age = S.next();
        System.out.println("请输入教师性别：");
        String sex = S.next();
        Teacher tea = new Teacher(id, name, age, sex);
        return tea;
    }
}
